/**
 *    不经过 前端请求拦截器和相应拦截器的请求 写在这里
 * @param data
 */

import axios from 'axios';
import { getToken } from './auth'

let xhr = new XMLHttpRequest();
xhr.open('get', 'serverConfig.json', false)
xhr.send()
let res = JSON.parse(xhr.responseText)

export function exportExcel(url, data, fileName) {
  const req = axios.create({
    baseURL: res.apiurl,
    params: data,
    headers: {
      'Accept-Language': 'zh-CN',
      'Content-Type': 'application/json;charset=UTF-8',
      'X-Version': '1.0',
      'X-Platform': '1',
      'X-Timestamp': new Date().getTime(),
      'X-Token': getToken()
    },
    timeout: 15000,
    responseType: 'blob'
  })


  return req.post(url).then(res => {
    let a = document.createElement('a');
    let blob = new Blob([res.data], { type: 'application/vnd.ms-excel;charset=utf-8' });
    
    a.href = URL.createObjectURL(blob);
    a.download = fileName + '.xls'
    a.click()
  })
}